import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:yt_devops/components/gaps.dart';
import 'package:yt_devops/components/log_utils.dart';
import 'package:yt_devops/components/yt_text_style.dart';
import 'package:yt_devops/pages/site/site_observer_detail/models/observer_model.dart';
import 'package:yt_devops/styles/styles.dart';

import 'observer_point_no_data.dart';

class SiteObserverListWidget extends StatefulWidget {
   List<ObserverModel> observerList;
   List<String> allObserverSelectName;
   List<String> allObserverShowName;
   List<ObserverModel> allObserverModelList;
  final Function(List<String> selectedlist) callBack;
   SiteObserverListWidget(this.observerList,this.allObserverSelectName,this.allObserverModelList,this.allObserverShowName,this.callBack, {Key? key}) : super(key: key);

  @override
  State<SiteObserverListWidget> createState() => _SiteObserverListWidgetState();
}

class _SiteObserverListWidgetState extends State<SiteObserverListWidget> {
  // List<ObserverModel> observerList = [];

  @override
  void initState() {
    // TODO: implement initState
    super.initState();

  }

  @override
  Widget build(BuildContext context) {
    Log.d('SiteObserverListWidget===' + widget.observerList.toString());
    if (widget.observerList.isNotEmpty) {

      for (var element in widget.observerList) {
        if(widget.allObserverSelectName.contains(element.selectName)){
          element.flag = true;
        }else{
          element.flag = false;
        }
      }
    }
    return widget.observerList.isEmpty? ObserverPointNoData(topPosition: 60,):ListView.builder(
        padding: EdgeInsets.zero,
        itemCount: widget.observerList.length,
        itemBuilder: (BuildContext context, int index) {
          return SizedBox(
            // height: 40,
            child: Column(
              children: [
               Container(
                 color: Colors.white,
                 child:  _itemWidget(widget.observerList[index],),),
                Container(color: Colour.dividerColor,height: 0.5,margin: const EdgeInsets.only(top: 2,bottom: 2),)
              ],
            )
          );
        });
  }

  _itemWidget(ObserverModel model) {
    return Row(
      children: [
        Gaps.hGap16,
        Expanded(
          child: Text(model.paramName!,
              maxLines: 10,
              overflow: TextOverflow.ellipsis,
              style: ytTextStyle(fontSize: 12,
                  color: (model.flag != null && model.flag! ==true)?Colour.primaryColor:Colour.mainTextColor
              ,fontWeight: (model.flag != null && model.flag! ==true)?FontWeight.w500:FontWeight.normal)),
        ),
       GestureDetector(
            // iconSize: 16,
            // padding: EdgeInsets.all(0),
            child: Container(
              padding: EdgeInsets.all(6),
              child: (model.flag != null && model.flag! ==true)
                  ? const Icon(
                size: 18,
                Icons.check_circle,
                color: Colour.primaryColor,
              )
                  : const Icon(
                size: 18,
                Icons.circle_outlined,
                color: Colour.diabledButtoBgColor,
              ),
            ),
            onTap: () {
              Log.d('onPressed===' + model.flag.toString());
              if( model.flag == null){
                model.flag = true;
                if(!widget.allObserverSelectName.contains(model.selectName)){
                  widget.allObserverModelList.add(model!);
                  widget.allObserverShowName.add(model.paramName!);
                  widget.allObserverSelectName.add(model.selectName!);
                }
              }else{
                if(model.flag){
                  model.flag = false;
                  if(widget.allObserverSelectName.contains(model.selectName)){
                    widget.allObserverSelectName.remove(model.selectName);
                    widget.allObserverShowName.remove(model.paramName!);
                    widget.allObserverModelList.removeWhere((element) => element.selectName == model.selectName);
                  }
                }else{
                  model.flag = true;

                  if(!widget.allObserverSelectName.contains(model.selectName)){
                    widget.allObserverSelectName.add(model.selectName!);
                    widget.allObserverShowName.add(model.paramName!);
                    widget.allObserverModelList.add(model!);
                  }
                }

              }
              setState(() {
              });
              widget.callBack(widget.allObserverShowName);
            },
          ),
      ],
    );
  }
}
